package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.cdn.dao.CdnCustomerInfoDao;

@Repository
public class CdnCustomerInfoDaoImpl implements CdnCustomerInfoDao {

	private static final Log log = LogFactory.getLog(CdnCustomerInfoDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCount(param);
		list = listPageData(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		
		if(StringUtils.equals(userLevel, "1") || StringUtils.equals(userLevel, "2")) {
			//sql = getSql();
			sql.append("select t.customerId,t.unitName,t.unitNature,t.domainNum,t.timstamp,t.cdnName,t.cdnId");
			sql.append(" from t_cdn_customerinfo_statistics t where 1 = 1");
			sql.append(" and t.province ='"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else {//高级权限
			sql.append("select t.customerId,t.unitName,t.unitNature,t.domainNum,t.timstamp,t.cdnName,t.cdnId");
			sql.append(" from t_cdn_customerinfo_statistics t where 1 = 1");
			sql.append(" and t.province ='"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append("select t.customerId,t.unitName,t.unitNature,t.domainNum,t.timstamp,t.cdnName,t.cdnId");
			sql.append(" from t_cdn_customerinfo_statistics t where 1 = 1");
			sql.append(" and t.province !='"+userProvince+"'");
			sql.append(" and t.cdnId in (select distinct house.cdnId from t_cdn_house_statistics house where house.houseProvince = '"+userProvince+"')");
			sql.append(getQueryConf(param));
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}

	public StringBuffer getSql() {
		StringBuffer sql = new StringBuffer();
		sql.append("select t.customerId,t.unitName,");
		sql.append("       case when t.unitNature = '1' then '军队' when t.unitNature = '2' then '政府机关' when t.unitNature = '3' then '事业单位' when t.unitNature = '4' then '企业' when t.unitNature = '5' then '个人' when t.unitNature = '6' then '社会团体' when t.unitNature = '999' then '其他' end as unitNature,");
		sql.append("	   (select count(*) from t_cdn_customer_service_domain domain where domain.cdnId = t.cdnId and domain.customerId = t.customerId) as domainNum,");
		sql.append("       t.timstamp as reportTime,cdn.cdnName,t.cdnId");
		sql.append(" from t_cdn_customerinfo t,t_cdn_cdndata cdn");
		sql.append(" where t.cdnId = cdn.cdnId");
		return sql;
	}
	
	public StringBuffer getSqlCount() {
		StringBuffer sql = new StringBuffer();
		sql.append("select t.unitName,t.unitNature,t.timstamp,cdn.cdnName,t.cdnId");
		sql.append(" from t_cdn_customerinfo t,t_cdn_cdndata cdn");
		sql.append(" where t.cdnId = cdn.cdnId");
		return sql;
	}
	
	public String listCount(Map<String, Object> param) {
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "3")) {
			sql.append("select sum(count) count from (");
			sql.append(" select count(1) count from t_cdn_customerinfo_statistics t where 1 = 1");
			sql.append(" and t.province ='"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(" select count(1) count from t_cdn_customerinfo_statistics t where 1 = 1");
			sql.append(" and t.province !='"+userProvince+"'");
			sql.append(" and t.cdnId in (select distinct house.cdnId from t_cdn_house_statistics house where house.houseProvince = '"+userProvince+"')");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}else {
			sql.append("select count(*) count from t_cdn_customerinfo_statistics t where 1 = 1");
			sql.append(" and t.province ='"+userProvince+"'");
			sql.append(getQueryConf(param));
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String cdnName = CommonUtils.valueOf(param.get("cdnName"));
		String customerId = CommonUtils.valueOf(param.get("customerId"));
		String reportTimeStart = CommonUtils.valueOf(param.get("reportTimeStart"));
		String reportTimeEnd = CommonUtils.valueOf(param.get("reportTimeEnd"));
		String cdnId = CommonUtils.valueOf(param.get("cdnId"));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer queryConf = new StringBuffer();
		
		/*if(!StringUtils.equals(userLevel,"3")) {
			if(StringUtils.isNotBlank(userProvince)) {
				queryConf.append(" and cdn.province ='"+userProvince+"'");
				if(StringUtils.equals(userLevel, "1")) {//初级权限
					queryConf.append(" and t.province='"+userProvince+"'");
				}else if(StringUtils.equals(userLevel, "2")) {//中级权限
					//中级权限企业下资源不需要限制
				}
			}
		}*/
		if(StringUtils.isNotBlank(cdnId)) {
			queryConf.append(" and t.cdnId = '"+cdnId+"'");
		}
		if(StringUtils.isNotBlank(cdnName)) {
			queryConf.append(" and t.cdnName like '%"+cdnName+"%'");
		}
		if(StringUtils.isNotBlank(customerId)) {
			queryConf.append(" and t.customerId like '%"+customerId+"%'");
		}
		if(StringUtils.isNotBlank(reportTimeStart)) {
			queryConf.append(" and t.timstamp >='"+reportTimeStart+"'");
		}
		if(StringUtils.isNotBlank(reportTimeEnd)) {
			queryConf.append(" and t.timstamp <='"+reportTimeEnd+"'");
		}
		return queryConf;
	}
}
